#include "Sock.hpp"
#include "Protocol.hpp"

void Usage(char *proc)
{
    cout << "Usage:" << proc << " ip "
         << " port " << endl;
    exit(1);
}

int main(int argc, char *argv[])
{
    if (argc != 3)
        Usage(argv[0]);

    // 1. 创建套接字
    int sock = Sock::Socket();
    // 2. 链接
    Sock::Connect(sock, argv[1], atoi(argv[2]));
    // 3. 发送服务需求
    request_t req;
    cout << "Please Enter Data One#";
    cin >> req.x;
    cout << "Please Enter Data Two#";
    cin >> req.y;
    cout << "Please Enter Operator#";
    cin >> req.op;
    string json_string = SerializeRequest(req);
    write(sock, json_string.c_str(), json_string.size());

    // 读取服务器响应
    char buffer[1024];
    responce_t resp;
    ssize_t s = read(sock, buffer, sizeof(buffer)-1);
    // 反序列化
    json_string = buffer;
    DeserializeResponce(json_string,resp); // 反序列化到resp
    cout << "code[0:success]:" << resp.code << endl;
    cout << "result:" << resp.result << endl;
    return 0;
}